<?php
namespace pay;
use  think\Request;
use think\Config;

class Benyuan{

    private  $callback;
    private $notify_url;
    public function __construct()
    {
        $class = strtolower("Benyuan");
        $this->Config  = $Config = Config::get($class);
            $domain = Request::instance()->domain();
            //curl实例
            $this->http =new \http\Http();
        if(!empty($Config['CallBackUrl'])){
            $this->callback = $Config['CallBackUrl'];
        }else{
            $this->callback = $domain."/out/Notify/callbackurl";
        }
        if(!empty($Config['NotificationUrl'])){
            $this->notify_url = $Config['NotificationUrl'];
        }else{
            $this->notify_url =$domain."/out/Notify/handle";
        }
    }

    /**
     * @param $deposit
     * @param $payNo
     * @param $CertName
     * @param $CertNo
     * @param $Phone
     * @return string  网关支付
     */
    function byPay($deposit,$payNo,$CertName,$CertNo,$Phone){
        $m = new \decrypt\Rsaclass();
        $data=array();
        $Request=[
            "PaymentNo"=>$payNo, //请求流水号
            "CertName"=>$CertName,//终端号
            "CertNo"=>$CertNo,//终端号
            "Phone"=>$Phone,//终端号
            "Amount"=>bcmul($deposit,100),//交易金额
        ];
        $TxCode  = $this->TxCodeCollection();
        $NEOBY0015  = $TxCode['NEOBY0015'];

        $NEOBY0015['Request']['Body']  = array_merge($NEOBY0015['Request']['Body'],$Request);
         $xml =  array2xml($NEOBY0015);
        $itrusHexEncry = $m->itrusHexEncry($xml);
        $data['signature'] = $m->signature($itrusHexEncry);
        $data['message'] = $m->message($xml);
        $data['itrusHexEncry'] = base64_encode($itrusHexEncry);
        $urls =  $this->Config['url'].http_build_query($data);
        return $urls;
    }


//用户绑卡 //四要素
    function tieCard($AccountNo,$CertName,$CertNo,$Phone){
        $result =[];
        $m = new \decrypt\Rsaclass();
        $Request=[
            "CertName"=>$CertName, //用户姓名
            "CertNo"=>$CertNo,//终端号
            "Mobile"=>$Phone,//终端号
            "AccountNo"=>$AccountNo,//商品名称
        ];
        $TxCode  = $this->TxCodeCollection();
        $NEOBY0013  = $TxCode['NEOBY0013'];
        $NEOBY0013['Request']['Body']  = array_merge($NEOBY0013['Request']['Body'],$Request);
        error_log("参数：".json_encode($NEOBY0013).PHP_EOL, 3, "/home/wwwlogs/NEOBY0013.log");
        $xml =  array2xml($NEOBY0013);
        $itrusHexEncry = $m->itrusHexEncry($xml);
        $data['signature'] = $m->signature($itrusHexEncry);
        $data['message'] = $m->message($xml);
        $data['itrusHexEncry'] = base64_encode($itrusHexEncry);
        $urls =  $this->Config['url'].http_build_query($data);
      //  error_log("加密的报文：".json_encode($data).PHP_EOL, 3, "/home/wwwlogs/NEOBY0013.log");
        $curl = new \http\Http();
      //  error_log($urls.PHP_EOL, 3, "/home/wwwlogs/bangkaURl.log");
    //    error_log("地址：".$urls.PHP_EOL, 3, "/home/wwwlogs/NEOBY0013.log");
      $res = $curl->get($urls);
  /* while(empty($res)){
          $res = $curl->get($urls);
      }*/
     //   error_log("返回的报文：".$res.PHP_EOL, 3, "/home/wwwlogs/NEOBY0013.log");
      //  error_log($res.PHP_EOL, 3, "/home/wwwlogs/bangka.log");
     // file_put_contents("1.log",$res);
       // $res = "0rqyiVbw3QeZqSs2heCIoLVylN3DkRDnPc17t+G3J1EWD3J3TYryCe7gyb8IELk51hslGsWKxApYDE2RE9DlrPvGS+O/apDm+86KAUlpKTIL3olSkVT9vlofu0p9B2W0iJFdVrO5rpp1Y6TbS087WSG6y28kh2shHz54W/srAjcLTkvZdvK569PaYgbZgOC33QocWDpvWM4pocEw66uuahgTwwVzmCVaYeiYUA5gFDzXDNjyU3lRPsH/ecP+D5m2R92SjmfiCQPTaUyMeG37VK9pIeIawSj+ck/eyyeIC4mxIGd7+o2R9jEgxdmxFx8eyaJ+rH9PeEFOcbO65SILyYMx1sZtCizJBH1woFl63QZeXgQMWFHDM40VL2sWEBsZL1/et7G+B9YGqF5YY5YgkD4xAhnjKeaIfCf3VSUDJOM/JrDtejuof/q0KYoQOliJApPED+9C5xX1vp6PeiYQtIpgxBFmtTVRD9lrhzUUiyQMU9wZENEkCjPPfCpVWB/N,u8SVHqpfwDXCjd1aZZn+n5j6Ej2rx5I6t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9qQeFCv9b7HUM7+ab373jpim2GCR9pB5iM9PRkfjIF/7u5Hb7umN72NHExixgOl3zL3ibtl7GoAkR6NeI3H1/zx9rHnhAQsZprshb+/odwbNCaRM+U61JeMUlf7+E,nbZ8NIv6DgdLYqCUYGfJrLpcJ8Mv4i5qXH1qaNuxarrarVRkJiptYiDevv8RMIQ+YaE2UxL6AwQSnPj75nj9BnhcJe5dtWjhXybnvJ8soqscsQIoVwKybxYfgDEcfk69NOiP+hIVOtPPV7fNw6ogGoD7iBbJw6YZtITrSiH44+g=,ixc1YUtZGP2lZ1wt5OymRJILDdM=";
        $res = explode(",",$res);

        if(!empty($res)){
            if(isset($res[1])){
                $data = [
                    "message"=>$res[0],
                    "signature"=>$res[1],
                    "itrusHexEncry"=>$res[2],
                ];

                $xml =  $m->dfdecrypt($data);

                $result = xmlToArray($xml);
                error_log("解析返回的数据：".json_encode($result).PHP_EOL, 3, "/home/wwwlogs/NEOBY0013.log");
            }
        }

        return $result;

    }

    /**
     * @return mixed 支付码集合
     */
    private function TxCodeCollection(){
        $data['Request']['Head'] = [
            "TxCode"=>"NEOBY0015"
        ];
        $data['Request']['Body'] =[
            "PaymentNo"=>"", //请求流水号
            "MerId"=>$this->Config['MerId'],//商户号
       /*     "SecondMerId"=>$this->Config['SecondMerId'],//二级商户号（收款商户编号）
            "TermId"=>$this->Config['TermId'],//终端号*/
            "CertName"=>"",//姓名
            "CertNo"=>"",//身份证号码
            "Phone"=>"",//电话号码
            "ItemName"=>'充值',//商品名称
            "IsMobile"=>"true",//
            "Amount"=>"",//交易金额
            "UserType"=>$this->Config['MerId'],
            "NotificationUrl"=>$this->notify_url,
            "CallBackUrl"=>$this->callback,
            "OrderTime"=>time(),
        ];
        $data2['Request']['Head'] = [
            "TxCode"=>"NEOBY0013" //新增代扣白名单
        ];
        $data2['Request']['Body'] =[
            "MerId"=>$this->Config['MerId'],//商户号
            "ReqSn"=>ordersn(),//流水号
            "CertName"=>"", //用户姓名
            "CertNo"=>"",//终端号
            "Mobile"=>"",//终端号
            "AccountNo"=>"",//商品名称
            "OptType"=>0,//
            "VerifyFlag"=>1,//
        ];
        $tData['NEOBY0015']=$data;//获取
        $tData['NEOBY0013']=$data2;//获取
        return $tData;
    }



}
?>
